package com.realbrand.net.realbrandpay.ui.activity.shopcar;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.realbrand.net.realbrandpay.R;
import com.realbrand.net.realbrandpay.adapter.MyLogisticAdapter;
import com.realbrand.net.realbrandpay.api.HTTPAPI;
import com.realbrand.net.realbrandpay.bean.LogisticBean;
import com.realbrand.net.realbrandpay.inter.ILogisticsInfo;
import com.realbrand.net.realbrandpay.untils.HttpRequest;
import com.realbrand.net.realbrandpay.untils.Static;
import com.realbrand.net.realbrandpay.zxing.util.L;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2018/9/7.
 */

public class LogisticFragment extends Fragment {

    Unbinder unbinder;
    String scan;
    List<LogisticBean> data;
    Context context;
    @BindView(R.id.fra_logistic_lv)
    ListView fraLogisticLv;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fra_logistic, container, false);
        unbinder = ButterKnife.bind(this, inflate);
        context = getActivity();
        scan = getArguments().getString("scan");
        return inflate;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        data = new ArrayList<>();
//        adapter = new LogisticAdapter(data, context);

        getLogistic(scan);
    }

    private void getLogistic(String scan) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("SOURCE", "BaiduMapActivity");
            jsonObject.put("CREDENTIALS", "0");
            jsonObject.put("TERMINAL", "1");
            jsonObject.put("INDEX", Static.getDay());
            jsonObject.put("METHOD", "LogisticsInfo");
            jsonObject.put("CommodityCode", scan);
            RequestBody body = RequestBody.create(HTTPAPI.JSON, jsonObject.toString());
            ILogisticsInfo iLogisticsInfo = HttpRequest.request_BASE_URL_VERSION(ILogisticsInfo.class, HTTPAPI.BASE_URL);
            iLogisticsInfo.getLogistic(body).enqueue(new Callback<ResponseBody>() {
                @Override
                public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                    try {
                        String result = response.body().string();
                        L.d("获取物流信息返回的结果" + result);
                        JSONObject object = new JSONObject(result);
                        JSONArray workAddress = object.getJSONArray("WorkAddress");
                        JSONArray enterpriseName = object.getJSONArray("EnterpriseName");
                        JSONArray readTime = object.getJSONArray("ReadTime");
                        JSONArray traces = object.getJSONArray("Traces");
                        JSONArray oddNumbers = object.getJSONArray("OddNumbers");
                        JSONArray shipperName = object.getJSONArray("ShipperName");
                        for (int i = 0; i < workAddress.length(); i++) {
                            if (readTime.getString(i).contains("入仓")) {
                                LogisticBean logisticBean = new LogisticBean();
                                logisticBean.setWorkAddress(workAddress.getString(i));
                                logisticBean.setEnterpriseName(enterpriseName.getString(i));
                                logisticBean.setTime(readTime.getString(i).substring(3, readTime.getString(i).length()));
                                logisticBean.setItem_type(1);
                                logisticBean.setOddNumbers(oddNumbers.getString(i));
                                logisticBean.setLogisticsName(shipperName.getString(i));
                                logisticBean.setTraces(traces.getString(i));
                                data.add(logisticBean);
                            } else if (readTime.getString(i).contains("出仓")) {
                                LogisticBean logisticBean = new LogisticBean();
                                logisticBean.setWorkAddress(workAddress.getString(i));
                                logisticBean.setEnterpriseName(enterpriseName.getString(i));
                                logisticBean.setTime(readTime.getString(i).substring(3, readTime.getString(i).length()));
                                logisticBean.setItem_type(2);
                                logisticBean.setOddNumbers(oddNumbers.getString(i));
                                logisticBean.setLogisticsName(shipperName.getString(i));
                                logisticBean.setTraces(traces.getString(i));
                                data.add(logisticBean);
                            }
                        }
                        MyLogisticAdapter myLogisticAdapter = new MyLogisticAdapter(data, context);
                        fraLogisticLv.setAdapter(myLogisticAdapter);
                    } catch (Exception e) {
                        e.printStackTrace();
                        L.d("物流信息异常" + e.toString());
                    }
                }

                @Override
                public void onFailure(Call<ResponseBody> call, Throwable t) {

                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
